import type { Module, ActionContext } from 'vuex'
import type { AxiosResponse } from 'axios'
import {
  hardwareListGet,
  hardwareRemovePost,
  getTaskGroupBoxListGet,
  getTaskPnGet,
  getWeekSunsierGet,
  hardwareAddPost,
  hardwareUpdatePost,
  hardwareViewGet
} from '~/api/monitor/switch'
import { getDataDictionaryByCode } from '~/api/global'
import type { SwitchStrategyStateType } from './typings'

const switchStrategy: Module<SwitchStrategyStateType, SwitchStrategyStateType> = {
  namespaced: true,
  state: {
    taskTypeOpt: [
      { dictKey: '1', dictValue: '周任务' },
      { dictKey: '2', dictValue: '多时段' },
      { dictKey: '3', dictValue: '全天' }
    ],
    circumferenceOpt: [
      { dictKey: 1, dictValue: '周日' },
      { dictKey: 2, dictValue: '周一' },
      { dictKey: 3, dictValue: '周二' },
      { dictKey: 4, dictValue: '周三' },
      { dictKey: 5, dictValue: '周四' },
      { dictKey: 6, dictValue: '周五' },
      { dictKey: 7, dictValue: '周六' }
    ],
    taskTimeSlotOpt: [
      { dictKey: 1, dictValue: '时段1' },
      { dictKey: 2, dictValue: '时段2' },
      { dictKey: 3, dictValue: '时段3' },
      { dictKey: 4, dictValue: '时段4' }
    ],
    dictTypeOpt: [],
    taskForceOpt: []
  },
  getters: {
    taskTypeOpt: (state) => state.taskTypeOpt,
    dictTypeOpt: (state) => state.dictTypeOpt,
    taskForceOpt: (state) => state.taskForceOpt,
    circumferenceOpt: (state) => state.circumferenceOpt,
    taskTimeSlotOpt: (state) => state.circumferenceOpt
  },
  mutations: {
    SET_DICT_TYPE(state, payload) {
      state.dictTypeOpt = payload
    },
    SET_TASK_FORCE(state, payload) {
      state.taskForceOpt = payload
    }
  },
  actions: {
    // 任务类型
    async DictTypeGet<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return await getDataDictionaryByCode(params).then((res: AxiosResponse) => {
        commit('SET_DICT_TYPE', res?.data?.data || [])
        return res?.data
      })
    },
    // 任务组
    async TaskForceGet<S, T>({ commit }: ActionContext<S, T>, params: T) {
      return await getTaskGroupBoxListGet(params).then((res: AxiosResponse) => {
        commit('SET_TASK_FORCE', res?.data?.data || [])
        return res?.data
      })
    },
    // 开关灯任务列表
    async HardwareListGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await hardwareListGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 开关灯任务列表-删除
    async HardwareRemovePost<S, T>(_: ActionContext<S, T>, params: T) {
      return await hardwareRemovePost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 开关灯任务列表-新增
    async HardwareAddPost<S, T>(_: ActionContext<S, T>, params: T) {
      return await hardwareAddPost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 开关灯任务列表-修改
    async HardwareUpdatePost<S, T>(_: ActionContext<S, T>, params: T) {
      return await hardwareUpdatePost<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 开关灯任务列表-详情
    async HardwareViewGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await hardwareViewGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 方案号
    async GetTaskPnGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await getTaskPnGet<T>(params).then((res: AxiosResponse) => res?.data)
    },
    // 周任务模式
    async GetWeekSunsierGet<S, T>(_: ActionContext<S, T>, params: T) {
      return await getWeekSunsierGet<T>(params).then((res: AxiosResponse) => res?.data)
    }
  }
}

export default switchStrategy
